using System;
using System.Collections.Generic;
using System.Text;

using Task.Contracts;

namespace Task
{
    /// <summary>
    /// Cryptography Task.
    /// </summary>
    public sealed class CryptographyTask : ICryptographyTask
    {
        #region ICryptographyTask Members

        /// <summary>
        /// Gets the MD5 from string.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <returns></returns>
        public string GetMd5FromString(string text)
        {
            string Password = text;
            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(Password);
            bs = x.ComputeHash(bs);
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            Password = s.ToString();
            return Password;
        }

        /// <summary>
        /// Gets the MD5 from user.
        /// </summary>
        /// <param name="u">The u.</param>
        /// <returns></returns>
        public string GetMd5FromUser(DataAccess.user u)
        {
            return GetMd5FromString(u.login + u.name + u.surname);
        }

        /// <summary>
        /// Gets the random string.
        /// </summary>
        /// <param name="length">The length.</param>
        /// <returns></returns>
        public string GetRandomString(int length)
        {
            try
            {
                string[] znaki ={"q","w","e","r","t","y","u","i","o","p",
                                "a","s","d","f","g","h","j","k","l",
                                "z","x","c","v","b","n","m","1","2","3","4","5","6",
                                "7","8","9","0"};
                string result = "";
                Random rand = new Random();
                for (int i = 0; i < length; i++)
                {
                    result += znaki[rand.Next(znaki.Length - 1)];
                }
                return result;
            }
            catch (Exception)
            {
                return "eeeeeee";
            }
        }

        #endregion ICryptographyTask Members
    }
}
